﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UILoginViewController : MonoBehaviour {

	public InputField inputUser;
	public InputField inputPwd;
	public Toggle toggleRemember;
	public Button buttonForget;
	public Button btnLogin;

	// Use this for initialization
	void Start () {
		btnLogin.onClick.AddListener(StartLoginHandler);	
	}

	private void StartLoginHandler()
	{
		Debug.Log("login");
		if(string.IsNullOrEmpty(inputUser.text.Trim()) || string.IsNullOrEmpty(inputPwd.text.Trim())){
			Global.Instance.Alert("用户名和密码不能为空!", "警告");
			return;
		}
		LoginReqeustVo vo = new LoginReqeustVo();
		vo.username = inputUser.text.Trim();
		vo.password = inputPwd.text.Trim();

		HttpHelper.Instance.SendRequest(HttpRequestId.login, OnLoginResultHandler, vo);
	}

	private void OnLoginResultHandler(bool success, string response, string msg, string json)
	{
		if (success)
		{
			Global.Instance.Alert("登录成功");
			Global.Instance.AccountInfo = JsonConvert.DeserializeObject<LoginResponseVo>(json);
		}
		else
		{
			Global.Instance.Alert(msg);
		}
	}

	// Update is called once per frame
	void Update () {
		
	}
}
